<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * E-Library Controller.
 * 
 * @class     Elibrary
 * @author    Iqbal
 * @version   1.0.0
 * @copyright Copyright (c) 2014 IPPI (http://www.ippi.or.id)
 */
class elibrary extends CI_Controller {
    /**
	 * Constructor.
	 */
    function __construct() {
        parent::__construct();
        
        // Load model
        $this->load->model('model_elibrary', 'm_elibrary', TRUE);
    }
    
    /**
	 * Load E-Library Data function.
     * 
     * @param[in] int $offset Offset of Table E-Library data
     * 
     * @return AJAX String
	 */
    function elibrary_data($offset=0){
        // Set Variables
        $limit          = get_option('elibrary_limit');
        $condition      = '';
        $search_val     = trim( ippi_isset($this->input->get('search_val'), '') );
        $search_fil     = trim( ippi_isset($this->input->get('search_fil'), '') );
        
        // Search E-Library
        if( !empty($search_val) && empty($search_fil) ) {
            $f          = '%judul% LIKE "%%s%%"';
            $condition .= str_replace('%s%', $search_val, $f);
        }elseif( !empty($search_fil) && empty($search_val) ) {
            $f          = '%kategori% = "%s%"';
            $condition .= str_replace('%s%', $search_fil, $f);
        }elseif( !empty($search_val) && !empty($search_fil) ){
            $f          = '%judul% LIKE "%%s%%"" AND %kategori% = "%s%"';
            $condition .= str_replace('%s%', $search_fil, $f);
        }
        
        // Get E-Library Data
        $elibrary_data  = $this->m_elibrary->get_all_elibrary_data($limit, $offset, $condition);
        $elibrary       = '';
        $pagination     = '';
        
        // Check for response get E-Library data
        if( !empty($elibrary_data) ){
            $total      = ippi_get_last_found_rows();
            
            foreach($elibrary_data as $row){
                $elibrary .= '
                <div class="col-sm-2">
                    <div class="thumbnail">
						<div class="media-wrap">
							<div class="border-transparent">
								<div class="overlay-hover">
									<div class="overlay-content">
										<a href="#" class="glyphicon icon-link lib_det" id="'.$row->id_elibrary.'"></a>
									</div>
								</div>
							</div>';
                            
                            if( !empty($row->image) ){
                                $elibrary .= '<img src="'.$row->image.'" />';
                            }else{
                                $elibrary .= '<img src="content/elibrary/cover/nocover.jpg" />';
                            }
                            
						$elibrary .= '
                        </div>
						<div class="caption">
							<div class="caption-inner" style="text-align: center;">
								<a href="' . base_url($row->file) . '" target="_blank" class="btn btn-primary btn-sm elibrary down-text">Download</a>
                                <a href="' . base_url($row->file) . '" target="_blank" class="btn btn-primary btn-sm elibrary down-icon"><span class="glyphicon glyphicon-save"></span></a>
							</div>
						</div>
					</div>
                </div>';
            }
            
            // Set Pagination
            if ($total > $limit){
                $this->load->library('pagination');
                
                $config['base_url']     = 'elibrary/elibrary_data/';
                $config['total_rows']   = $total;
                $config['per_page']     = $limit; 
                $config['anchor_class'] = 'class="pagination_elibrary" ';
                
                $this->pagination->initialize($config);
                $pagination             = $this->pagination->create_links(); 
            }
        }else{
            $elibrary . '
            <div class="col-md-12">
                <div class="show-more">
                    <div class="show-more-inner text-center">
                        <a href="#" class="btn-showmore btn btn-primary btn-sm disabled">No E-Libraries</a>
                    </div>
                </div>
            </div>';
        }
        
        // Set JSON data
        $data = array(
            'elibrary'      => $elibrary,
            'pagination'    => $pagination
        );
        
        // JSON encode data
        die(json_encode($data));
    }
    
    /**
	 * Get Donation details by E-Library ID function. 
     * @return AJAX String
	 */
    function elibrary_details(){
        // Set E-Library ID param
        $id_elibrary        = ippi_isset($this->input->post('id_elibrary'), '');
        $id_elibrary        = absint($id_elibrary);
        
        // Get E-Library Details
        $elibrary           = $this->m_elibrary->select_by_id($id_elibrary, 'row');
        
        // Set category of E-Library
        if($elibrary->kategori == 'report')          { $kat = 'Annual Report'; }
        elseif($elibrary->kategori == 'info')        { $kat = 'Info'; }
        elseif($elibrary->kategori == 'dasarhukum')  { $kat = 'Dasar Hukum'; }
        
        // Return HTML
        echo '
        <div class="quote" style="margin-bottom: 0px;">
            <blockquote style="margin-bottom: 10px;">
                <h5 style="margin: 10px 0 0 0 !important; line-height: 24px;">'.$elibrary->judul.'</h5>
                <dl class="dl-inline">
                    <dt>Category :</dt>
                    <dd style="margin-bottom: 5px;">'. $kat . '</dd>
                </dl>
            </blockquote>
        </div>
        <div class="well">' . $elibrary->deskripsi . '
            <p><a href="' . base_url($elibrary->file) . '" target="_blank" class="btn btn-primary btn-sm">Download</a></p>
        </div>';
    }
}

/* End of file elibrary.php */
/* Location: ./application/controllers/elibrary.php */